package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.proCarryOut.ProCarryOutDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProExpendDto;
import com.dg.cuckoo.admin.repository.ProCarryOutRepository;
import com.dg.cuckoo.admin.repository.ProExpendRepository;
import com.dg.cuckoo.admin.service.ExpendProphaseService;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExpendProphaseServiceimpl implements ExpendProphaseService {

    @Autowired
    private ProExpendRepository repository;
    @Autowired
    private ProCarryOutRepository proCarryOutRepository;

    @Override
    public void save(String projectId, ProExpendDto dto) {
        powerfulSave(1,projectId,dto);
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
    }

    @Override
    public ProExpendDto openInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        System.err.println(id);
        return repository.findById(id).get();
    }

    @Override
    public void powerfulSave(Integer type, String projectId, ProExpendDto dto) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        checkSave(dto);
        dto.setProjectId(projectId);
        dto.setType(type);
        repository.save(dto);
    }

    @Override
    public void powerfulUpdate(Integer type, String id, String projectId, ProExpendDto dto) {
        if(id==null){
            throw new ServiceException("请选择id");
        }
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        dto.setId(id);
        dto.setProjectId(projectId);
        dto.setType(type);
        repository.save(dto);
    }

    @Override
    public PageModel powerfulPage(Integer type, Integer page, Integer pageSize, String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findByProjectIdAndType(projectId, 1).size();
        List<ProExpendDto> dtos = repository.page(type, projectId, page, pageSize);
        PageModel model = new PageModel(one, pageSize, size, dtos);
        return model;
    }

    @Override
    public void update(String id, String projectId, ProExpendDto dto) {
        powerfulUpdate(1,id,projectId,dto);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize, String projectId) {
        return powerfulPage(1,page,pageSize,projectId);
    }

    @Override
    public List<String> openContract(String projectId) {
        List<ProCarryOutDto> dtos = proCarryOutRepository.findByProjectId(projectId);
        List<String> list=new ArrayList<>();
        for (ProCarryOutDto p:dtos){
            list.add(p.getContract());
        }
        return list;
    }

    public void checkSave(ProExpendDto dto){

    }
}
